﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Asxsyd92Core.Utils;
using Asxsyd92Core.Utils.Data.SQLServer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WebOS.Bll;
using WebOS.Dal;

namespace WebOSCore.Controllers.CommonTask
{
    public class CommonTaskController : Controller
    {
        // /Tasks/WaitList
        [Route("Tasks/WaitList")]
        [HttpGet]
        [Authorize]
        public string WaitList(string title, Guid type, Guid user, DateTime date1, DateTime date2,string desc="ID", int page=1, int limit=15)
        {
            //根据用户获取待办

            var id = User.Claims.FirstOrDefault(c => c.Type == "id").Value;
            Guid guid;
            Guid.TryParse(id, out guid);
            return CommonTaskBll.Instance.WaitList(guid, title, user, date1, date2, desc,page, limit);

        }
        [HttpPost]
        [Route("Tasks/DelFormData")]
        public string DelFormData(Guid key, string table)
        {
            if (table.IsNullOrEmpty())
                return JSONhelper.ToJson(new { msg = "表数据为空不能删除！", Success = false });
            else
            {
                //删除主表数据成功之后删除业务表中数据，附件在附件管理中删除
                int tag = CommonTaskDal.Instance.Delete(new { InstanceID = key });

                if (tag > 0)
                {

                    var tag0 = SqlFromData.DelFromData(table, key);
                    if (tag0 > 0)
                    {
                        return JSONhelper.ToJson(new { msg = "删除成功！", Success = true });
                    }
                    else
                    {
                        return JSONhelper.ToJson(new { msg = "删除成功,但未能彻底删除！", Success = true });
                    }
                }
                return JSONhelper.ToJson(new { msg = "删除失败！", Success = false });

            }

        }
        [HttpGet]
        [Route("Tasks/GetFormData")]
        public string GetFormData(string table, string where)
        {

            return JSONhelper.ToJson(WebOS.Dal.CommonTaskDal.Instance.GetNoMoldeWhere(table, where));
        }
    }
}